package miaofusdk

import (
	"encoding/json"
	"errors"
	"fmt"
	"gitee.com/garlic/tools"
)

/**
老的支付宝授权..不准备用了...
暂时只做数据中转
*/
type OldAliTokenBean struct {
	Id           int    `json:"id,omitempty,string"`
	ProdAppId    string `json:"prod_app_id,omitempty"`
	UserId       string `json:"user_id,omitempty"`
	AuthToken    string `json:"auth_token,omitempty"`
	AppId        string `json:"app_id,omitempty"`
	Expires      int64  `json:"expires,omitempty,string"`
	ReExpires    int64  `json:"re_expires,omitempty,string"`
	RefreshToken string `json:"refresh_token,omitempty"`
}

type RespOldAliToken struct {
	Response
	Data OldAliTokenBean `json:"data,omitempty"`
}

func (s *SrvShopManager) GetTokenByAliId(ali_id string) (OldAliTokenBean, error) {
	resp := RespOldAliToken{}
	http_request := s.GetPost(s.GetUrl() + "/old_alitoken/query")
	http_request.Param("ali_id", ali_id)
	if v, err := http_request.Bytes(); err != nil {
		return resp.Data, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp.Data, err
	} else if resp.Code != "0" {
		return resp.Data, errors.New(resp.Msg)
	} else {
		return resp.Data, nil
	}
}

type AlipayMchAuthBean struct {
	Id                 int
	Appid              string
	AppPid             string
	AppName            string
	Scope              string
	AccessToken        string
	UserId             string
	AuthAppId          string
	ExpiresIn          int64
	ReExpiresIn        int64
	RefreshToken       string
	Avatar             string
	NickName           string
	Province           string
	City               string
	Gender             string
	UserTypeValue      string
	UserStatus         string
	FirmName           string
	RealName           string
	Email              string
	CertNo             string
	Phone              string
	Mobile             string
	IsCertified        string
	IsBankAuth         string
	IsIdAuth           string
	IsMobileAuth       string
	IsLicenceAuth      string
	IsStudentCertified string
	IsCertifyGradeA    string
	CertTypeValue      string
	DeliverPhone       string
	DeliverMobile      string
	DeliverFullname    string
	Area               string
	Address            string
	Zip                string
	AuthType           string
}

func (s *SrvShopManager) UpdateToken(token AlipayMchAuthBean, shop_code string) (Response, error) {
	resp := Response{}
	http_request := s.GetPost(s.GetUrl() + "/old_alitoken/update")
	http_request.Param("shop_code", shop_code)
	m := tools.JsonToMap(token)
	for k, _ := range m {
		value := fmt.Sprintf("%v", m[k])
		if value != "" {
			http_request.Param(k, value)
		}
	}
	if v, err := http_request.Bytes(); err != nil {
		return resp, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp, err
	} else {
		return resp, nil
	}
}
